package com.jt.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import javax.jws.Oneway;
import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @outhor fufeixiang
 * @date 2021-04-26
 */
@Component
//@Aspect
public class SpringAOP {

//    @Before("bean(userServiceImpl)")
//    @Before("within(com.jt..*.userServiceImpl)")
//    @Before("execution(* com.jt..*.*(..))")
//    @Before("@annotation(com.jt.anno.aaa)")

    @Pointcut("@annotation(com.jt.anno.aaa)")
    public void pointCut(){

    }
    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
        System.out.println("获取目标对象的类型："+joinPoint.getTarget().getClass());
        System.out.println("获取目标对象的类名："+joinPoint.getSignature().getDeclaringTypeName());
        System.out.println("获取目标对象的方法名："+joinPoint.getSignature().getName());
        System.out.println("获取目标对象的方法参数："+ Arrays.toString(joinPoint.getArgs()));
        System.out.println("我是before通知");
    }

    @AfterReturning(pointcut = "pointCut()" ,returning ="result")
    public void afterReturing(Object result){
        System.out.println("返回的信息为："+result);
        System.out.println("我是afterReturing通知");
    }
    @AfterThrowing(pointcut = "pointCut()",throwing ="e" )
    public void afterThrowing(Exception e){

            System.out.println("获取异常的信息:"+e.getMessage());
            System.out.println("获取异常的类型："+e.getClass());
            System.out.println("我是afterThrowing通知");
    }
    @After("pointCut()")
    public void after(){
        System.out.println("我是after通知");
    }
    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint){
        Object result = null;
        try {
            System.out.println("开始表演");
            result = joinPoint.proceed();
        }catch (Throwable throwable){
            throwable.printStackTrace();
        }
        System.out.println("结束表演");
        return result;
    }
}
